//
//  StockHeaderView.m
//  quxiangtao
//
//  Created by HHuajiang on 2018/11/19.
//  Copyright © 2018 shanhaijin. All rights reserved.
//

#import "StockHeaderView.h"

@interface StockHeaderView ()
@property (nonatomic, weak) UIView *bbbbbView;
@property (strong, nonatomic) UIButton *selectedBtn;
// 用于标记在价格是选中状态下，显示不同的图片  YES 表示价格升序  NO表示价格降序
@property (assign, nonatomic) BOOL priceIsSelected;
@end

@implementation StockHeaderView

- (instancetype)init {
    if (self = [super init]) {
        self.backgroundColor = Colorf2f2f2;
        UIView *bgView = [[UIView alloc] init];
        bgView.backgroundColor = [UIColor whiteColor];
        [self addSubview:bgView];
        [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(Margin);
            make.right.mas_equalTo(-Margin);
            make.top.bottom.mas_equalTo(0);
        }];
        
        self.bbbbbView = bgView;
        
        
        
        [self createWithtitle:@"综合" image:nil withTag:0];
        [self createWithtitle:@"销量" image:nil withTag:1];
        [self createWithtitle:@"价格" image:[UIImage imageNamed:@"priceNormal"] withTag:2];
        [self createWithtitle:@"优惠" image:nil withTag:3];
        
    }
    return self;
}
- (void)createWithtitle:(NSString *)name image:(UIImage *)image withTag:(NSInteger)tag {
    
    UIButton *btn = [[UIButton alloc] init];
    btn.titleLabel.font = Font(24);
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [btn setTitle:name forState:UIControlStateNormal];
    [btn setTitleColor:Color333333 forState:UIControlStateNormal];
    [btn setTitleColor:Colorff0000 forState:UIControlStateSelected];
    
    btn.tag = tag;
    
    CGFloat width = jisuan(80);
    CGFloat margin = (WIDTH - SmallMargin * 2 - Margin * 2 - width * 4) / 3.0;
    CGFloat x = SmallMargin + (width + margin) * tag;
    btn.frame = CGRectMake(x, 0, width, jisuan(60));
    [self.bbbbbView addSubview:btn];
    
    if (image) {
        
        [btn setImage:image forState:UIControlStateNormal];
    }
    
    if (tag == 0) {
        
        // 设置“综合”为默认选中状态
        self.selectedBtn = btn;
        btn.selected = YES;
        //        self.selectedBtn.selected  = NO;
        
    }else {
        
        btn.selected = NO;
        
    }
    
    
    if (tag == 0) {
        
        // 综合
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        
    }else if (tag == 3) {
        
        // 筛选
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - btn.frame.size.width + btn.titleLabel.frame.size.width + jisuan(15), 0, 0);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -btn.titleLabel.frame.size.width - btn.frame.size.width +  jisuan(20));
        
    }else if (tag == 2) {
        
        // 价格
        
        btn.titleEdgeInsets = UIEdgeInsetsMake(0, -btn.imageView.frame.size.width - btn.frame.size.width + btn.titleLabel.frame.size.width + jisuan(15), 0, 0);
        btn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -btn.titleLabel.frame.size.width - btn.frame.size.width + jisuan(20));
        
    }
    
    
    if (tag == 2) {
        
        // 价格
        [btn setImage:[UIImage imageNamed:@"priceUp"] forState:UIControlStateSelected];
        
    }else if (tag == 3) {
        
        // 筛选
        //        [btn setImage:[UIImage imageNamed:@"filterSelected"] forState:UIControlStateSelected];
        
    }
    
    [btn addTarget:self action:@selector(headerViewBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
}

#pragma mark -分栏按钮点击事件
- (void)headerViewBtnClick:(UIButton *)btn {
    
    
    if (btn!= self.selectedBtn) {
        
        self.selectedBtn.selected = NO;
        
        btn.selected = YES;
        self.selectedBtn = btn;
        
    }else{
        
        if (btn.tag == 0 || btn.tag == 1 || btn.tag == 3) {
            
            return;
            
        }
        
    }
    
    if (btn.tag == 0) {
        
        NSLog(@"综合");
        self.priceIsSelected = YES;
        
        // 综合数据请求
        if (self.stockHeaderViewBlock) {
            self.stockHeaderViewBlock(@"1");
        }
        
        
    }else if (btn.tag == 1) {
        
        NSLog(@"销量");
        self.priceIsSelected = YES;
        
        // 销量数据请求
        if (self.stockHeaderViewBlock) {
            self.stockHeaderViewBlock(@"2");
        }
        
    }else if (btn.tag == 2) {
        
        if (self.priceIsSelected) {
            
            NSLog(@"升序");
            
            [btn setImage:[UIImage imageNamed:@"priceUp"] forState:UIControlStateSelected];
            
            self.priceIsSelected = NO;
            
            // 价格升序数据请求
            if (self.stockHeaderViewBlock) {
                self.stockHeaderViewBlock(@"3");
            }
            
        }else {
            
            NSLog(@"降序");
            
            [btn setImage:[UIImage imageNamed:@"priceDown"] forState:UIControlStateSelected];
            
            self.priceIsSelected = YES;
            
            // 价格降序数据请求
            if (self.stockHeaderViewBlock) {
                self.stockHeaderViewBlock(@"4");
            }
            
        }
        
        
    }else  {
        
        NSLog(@"筛选");
        self.priceIsSelected = YES;
        
        
        // 筛选数据请求
        
        if (self.stockHeaderViewBlock) {
            self.stockHeaderViewBlock(@"5");
        }
        
    }
        
}



@end
